{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串\n",
    "s1 = 'hello'\n",
    "s2 = \"hello\"\n",
    "s3 = \"\"\"hello\"\"\"\n",
    "s4 = '''hello'''\n",
    "s1 == s2 == s3 == s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"I'm a Python programer\""
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"I'm a Python programer\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'I\\'m a Python programer'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\tc\n"
     ]
    }
   ],
   "source": [
    "#转义字符\n",
    "s = 'a\\nb\\tc'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name[0] = 'j'\n",
      "name[1:3] = 'as'\n"
     ]
    }
   ],
   "source": [
    "# 字符串的切片\n",
    "name = 'jason'\n",
    "print(f\"{name[0] = }\")\n",
    "print(f\"{name[1:3] = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "j\n",
      "a\n",
      "s\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "for char in name:\n",
    "    print(char)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m--\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
      "\u001b[0;32m<ipython-input-63-b2cadb1da399>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'hello'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'H'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "s = 'hello'\n",
    "s[0] = 'H'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello'"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.replace('h','H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = s.replace('h','H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello'"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(x) = 140662510689776\n",
      "id(x) = 140662570711664\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'10'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字符串的 += 拼接 \n",
    "x = \"1\"\n",
    "print(f\"{id(x) = }\")\n",
    "x += '0'  #相当于 x = x +'0'\n",
    "print(f\"{id(x) = }\")\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(x) = 140662510689776\n",
      "id(x) = 140662570712752\n"
     ]
    }
   ],
   "source": [
    "x = '1'\n",
    "print(f\"{id(x) = }\")\n",
    "x = x + '0'\n",
    "print(f\"{id(x) = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 'hive:'\n",
      "b = 'ads/training_table'\n",
      "namespace = 'ads'\n",
      "table = 'training_table'\n"
     ]
    }
   ],
   "source": [
    "#字符串的分割\n",
    "\n",
    "path = 'hive://ads/training_table'\n",
    "a,b = path.split('//')\n",
    "print(f\"{a = }\")\n",
    "print(f\"{b = }\")\n",
    "\n",
    "namespace = b.split('/')[0] # 返回'ads'\n",
    "table = b.split('/')[1] # 返回 'training_table'\n",
    "print(f\"{namespace = }\")\n",
    "print(f\"{table = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s.strip() = '编 程 小 妙 招'\n",
      "s.lstrip() = '编 程 小 妙 招 '\n",
      "s.rstrip() = ' 编 程 小 妙 招'\n",
      "s.replace(' ','') = '编程小妙招'\n"
     ]
    }
   ],
   "source": [
    "s = ' 编 程 小 妙 招 '\n",
    "print(f\"{s.strip() = }\")\n",
    "print(f\"{s.lstrip() = }\")\n",
    "print(f\"{s.rstrip() = }\")\n",
    "print(f\"{s.replace(' ','') = }\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is somenzz, use python\n",
      "my name is somenzz, use python\n",
      "my name is somenzz, use python\n",
      "my name is name = 'somenzz', use language ='python'\n"
     ]
    }
   ],
   "source": [
    "# 字符串的格式化\n",
    "\n",
    "name = 'somenzz' \n",
    "language = 'python'\n",
    "\n",
    "print(\"my name is %s, use %s\" % (name, language))\n",
    "\n",
    "print(\"my name is {}, use {}\".format(name, language))\n",
    "print(f\"my name is {name }, use {language}\")\n",
    "print(f\"my name is {name = }, use {language =}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [Python 的 f-strings 作用远超你的预期](https://mp.weixin.qq.com/s/y8p29I4U9fZVItJUY-bFuA)\n",
    "\n",
    "## [Python 基础系列--字符串与编码](https://mp.weixin.qq.com/s/8g59YmbVbOTilL4bB0GlwQ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
